﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.BidSystem.Model.View;
using System.Data;

public partial class Company_BargainPriceSelect : System.Web.UI.Page, IAccess
{
    string action = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentAccount.CheckSessionTimeOut(this);
        action = Request.QueryString["action"] == null ? "bargain" : Request.QueryString["action"].ToLower();

        int b = BLL_Factory.IBLL_Common.GetBargain(int.Parse(CurrentAccount.Account.AccountId.ToString()));


        if (!CurrentProject.IsBargainSwitch)
        {
            HZ.Web.MessageBox.Show("议价开关未开启，暂时无法查看。");
        }

        if (CurrentProject.InBargainTime2 < 0)
        {
            HZ.Web.MessageBox.Show("议价时间未到，请稍后查看！");
        }

        if (CurrentProject.InBargainTime2 > 0)
        {
            HZ.Web.MessageBox.Show("议价时间已过，无法查看");
        }
        //if (CurrentAccount.IsConfirmBid)
        //{
        //    Response.Redirect(string.Format("PrePrintPrice.aspx?action={0}", action));
        //}
        if (!IsPostBack)
        {
            bidinfo.Text = BLL_Factory.IBLL_Manage.GetConfigurationByKey("BargainDes").Value;
            BindData();
        }
        WebTimer1.EndTime = CurrentProject.ProjectConfig.BargainEndTime.Value;
        WebTimer1.BeginTime = CurrentProject.ProjectConfig.BargainStartTime.Value;

    }

    /// <summary>
    /// 确认
    /// </summary> 
    protected void LinkButtonGet_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        // Repeater rptGMP = e.Item.FindControl("rptGMP") as Repeater;
        if (btn.CommandName == "Yes")
        {
            string bidId = btn.CommandArgument.Split(',')[0];
            string CompanyID_TB = btn.CommandArgument.Split(',')[1];
            View_BargainPrice2 goods = new View_BargainPrice2(); //记录日志用
            goods.BidId = bidId;
            goods.CompanyId_Tb = Convert.ToInt32(CompanyID_TB);
            goods.Switch = 1;
            int CheckSecces = BLL_Factory.IBLL_Company.UpdateBarinPrice(goods);
            if (CheckSecces == 1)
            {
                HZ.Common.log4netManager.LogInfo("企业确认第一轮价格", string.Format("企业确认第一轮价格：企业编号：{0}，报价IP：{1}，报价产品：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, bidId), null);
                HZ.Web.WebUtil.Alert("操作成功！", this);
                BindData();
            }
            else
            {
                HZ.Web.WebUtil.Alert("操作失败，请检查数据！", this);
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        pager1.CurrentPage = 1;
        BindData();
    }
    protected void BindData()
    {
        int totalrecord;
        string append = "1=1 and Switch = 1";
        DataTable dt = BLL_Factory.IBLL_Company.GetBarinBidPrice(new HZ.BidSystem.Model.QBidPrice() { CompanyId_Tb = CurrentAccount.Account.AccountId, SortFields = "Company_Goods", ProductName = txtProdcutName.Text, Model = txtModel.Text, Outlookc = txtOutlookc.Text, Append = append }, pager1.CurrentPage, pager1.PageSize, out totalrecord, CurrentAccount.BidPwd, CurrentAccount.RandPwd);
        pager1.RecordCount = totalrecord;
        if (dt.Rows.Count == 0)
        {
            btnSubmitPrice.Enabled = false;
        }
        else
        {
            btnSubmitPrice.Enabled = true;
        }
        gvbidprice.DataSource = dt;
        gvbidprice.DataBind();

    }

    protected void pager1_PageChanging(object sender, HZ.Web.PageChaningEventArgs e)
    {
        BindData();
    }

    /// <summary>
    /// 获取状态
    /// </summary> 
    public string GetBarState(object o)
    {
        string id = o.ToString(), value = "";

        switch (id)
        {
            case null:
                value = "未处理";
                break;
            case "":
                value = "未处理";
                break;
            case "0":
                value = "未处理";
                break;
            case "1":
                value = "已确认专家出价"; 
                break;
            case "2":
                value = "已还价";
                break;
        }
        return value;
    }


    /// <summary>
    /// 获取状态
    /// </summary> 
    public string GetBarStateSelect(object o)
    {
        string id = o.ToString(), value = "";

        switch (id)
        {
            case null:
                value = "未处理";
                break;
            case "":
                value = "未处理";
                break;
            case "0":
                value = "未处理";
                break;
            case "1":
                value = "拟中标";
                break;
            case "2":
                value = "已还价";
                break;
        }
        return value;
    }

    protected void btnSelect_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void gvbidprice_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //   // CheckBox chkbox = e.Row.FindControl("chkConfirm") as CheckBox;
        //    RadioButtonList rablt = e.Row.FindControl("rblisok") as RadioButtonList;
        //    var datasource = e.Row.DataItem as View_BargainPrice;

        //    if (datasource.BargainEncryptPrice != null && datasource.ExpertPrice != null && !datasource.BargainEncryptPrice.ToString().Equals("") && datasource.ExpertPrice.Value>0)
        //    { 
        //        if (datasource.BargainDecPrice.Value > 0) rablt.Items[0].Selected = true;
        //        if (datasource.BargainDecPrice.Value < 1) rablt.Items[1].Selected = true;
        //    }
        //}
    }

    #region IAccess Members

    IList<EnumAccess> IAccess.AccessList
    {
        get
        {
            var list = new List<EnumAccess>();
            list.Add(EnumAccess.Company);
            return list;
        }

    }

    #endregion
    //弃用方法
    protected void btnSubmitPrice_Click(object sender, EventArgs e)
    {
        IList<int> bidids = new List<int>();
        IList<double> bidprices = new List<double>();
        IList<string> bidpricesshuoming = new List<string>();

        //第一次企业议价
        IList<int> bidcheck = new List<int>();

        System.Text.StringBuilder sbmsg = new System.Text.StringBuilder();
        foreach (GridViewRow item in gvbidprice.Rows)
        {
            if (item.RowType == DataControlRowType.DataRow)
            {
                string strprice = (item.FindControl("txtBidPrice") as TextBox).Text.Trim();
                string BidPriceDcresbution = (item.FindControl("txtBidPriceDcresbution") as TextBox).Text.Trim();
                BidPriceDcresbution = HZ.Fun.InputText(BidPriceDcresbution, BidPriceDcresbution.Length);
                string goodsid = item.Cells[1].Text;

                //第一次议价时确认当前页所有数据必须选择一个状态（同意或者不同意）
                bool bcheck0 = ((RadioButtonList)item.FindControl("rblisok")).Items[0].Selected;//同意状态
                bool bcheck1 = ((RadioButtonList)item.FindControl("rblisok")).Items[1].Selected;//不同意状态

                if (bcheck0 || bcheck1)//有意见
                {
                    var bidid = new HZ.Common.ConvertHelper(item.Cells[0].Text).ToInt32;
                    if (bidid == null)
                    {
                        continue;
                    }
                    else
                    {
                        bidids.Add(bidid.Value);
                        if (bcheck0) bidcheck.Add(1);
                        if (bcheck1) bidcheck.Add(0);

                    }
                }


                if (HZ.Fun.IsUFloat(strprice))
                {
                    var bidid = new HZ.Common.ConvertHelper(item.Cells[0].Text).ToInt32;
                    if (bidid == null)
                    {
                        continue;
                    }
                    else
                    {
                        bidids.Add(bidid.Value);
                        bidprices.Add(new HZ.Common.ConvertHelper(strprice).ToDouble.Value);
                        bidpricesshuoming.Add(BidPriceDcresbution.ToString());
                    }

                }
                else //无意见
                {
                    //if (strprice != "")
                    //{
                    sbmsg.Append(string.Format("流水号为{0}的品规，价格格式不是数字！。\\r\\n", goodsid));
                    // }

                    continue;
                }
            }
        }
        //第一次议价时确认当前页所有数据必须选择一个状态，否则不给提交
        //if (bidids.Count != gvbidprice.Rows.Count)//如果提交总行数和总页面总行数不符则不然提交
        //{
        //    sbmsg.Append("本页议价提交失败！");
        //    HZ.Web.WebUtil.Alert(sbmsg.ToString(), this);
        //    return;
        //}
        if (sbmsg.Length > 0)//如果提交总行数和总页面总行数不符则不然提交
        {
            //sbmsg.Append("本页议价提交失败！");
            HZ.Web.WebUtil.Alert(sbmsg.ToString(), this);
            // return;
        }
        if (bidids.Count > 0)
        {
            BLL_Factory.IBLL_Company.BargainPrice(bidids, bidprices, bidpricesshuoming);
            //BLL_Factory.IBLL_Company.BargainPriceCheck(bidids, bidcheck);
            HZ.Common.log4netManager.LogInfo("企业提交议价", string.Format("企业提交议价：企业编号：{0}，报价IP：{1}，报价产品：{2}，价格：{3}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, Util.GetString(bidids), Util.GetString(bidprices)), null);

        }
        sbmsg.Append("本页议价提交成功");
        HZ.Web.WebUtil.Alert(sbmsg.ToString(), this);
        BindData();
    }
}
